<?php
/**
 * 留言本app模块
 * 
 * @version $Id$
 */

class Module_message
{
	/**
	 * 构造函数
	 */
	function __construct() {
		
	}

	
	function index() {
		return $this->showlist();
	}
	
	/**
	 * 用户的游戏列表
	 */
	function showlist() {
		$uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		
		// 如果没有uin参数，则显示自己的个人主页
		if(empty($uin)) {
			$uin = SessionInfo('uin');
		}
		// 获取用户数据,用于判断uin用户内容是否存在
		loadLib('User');
		$userinfo = USER::getFullInfo($uin);
		if(empty($userinfo)){
			return View::error('该用户信息不存在' );
		}
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		if($page < 1) {
			$page = 1;
		}
		
		$smarty = loadSmarty();
		
		$is_own = $uin == SessionInfo('uin');
		$smarty->assign('is_own' , $is_own);
		
		// 获取数据
		$per_page = 10;
		$offset = ($page - 1) * $per_page;
		
		loadLib('Message');
		$message_count = Message::getCount($uin);
		
		$message_list = Message::getList($uin , $offset , $per_page);
		$send_uin_list = array();
		foreach($message_list as $row) {
			$send_uin_list[] = $row['send_uin'];
			
			if(!empty($row['reply'])) {
				foreach($row['reply'] as $reply_row) {
					$send_uin_list[] = $reply_row['uin'];
				}
			}
		}
		
		if(!empty($send_uin_list)) {
			loadLib('User');
			$send_uin_info = User::getBaseInfo($send_uin_list);
			
			foreach($message_list as $k => $row) {
				$send_uin = $row['send_uin'];
				$message_list[$k]['send_uin_name'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['name'] : '';
				$message_list[$k]['send_uin_sex'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['sex'] : '';
				
				if(!empty($row['reply'])) {
					foreach($row['reply'] as $j => $reply_row) {
						$reply_uin = $reply_row['uin'];	
						$message_list[$k]['reply'][$j]['name'] = isset($send_uin_info[$reply_uin]) ? $send_uin_info[$reply_uin]['name'] : '';
						$message_list[$k]['reply'][$j]['sex'] = isset($send_uin_info[$reply_uin]) ? $send_uin_info[$reply_uin]['sex'] : '';
					}
				}
			}
		}
		
		$smarty->assign('message_count' , $message_count);
		$smarty->assign('per_page' , $per_page);
		$smarty->assign('message_list' , $message_list);
		
		// 准备个人主页导航数据
		View::prepare_profile_nav_data($uin);
		
		$smarty->assign('owner_uin' , $uin);
		
		return $smarty->fetch('message/showlist.tpl.php');
	}
	
	/**
	 * 添加留言
	 */
	function add() {
		$owner_uin = isset($_POST['owner_uin']) ? intval($_POST['owner_uin']) : null;
		$content = isset($_POST['content']) ? trim($_POST['content']) : null;
		
		if(empty($owner_uin)) {
			return View::error('参数错误');
		}
		
		// 检查留言有效性
		if($content === null || strlen($content) == 0) {
			return View::error('没有填写内容');
		}
		
		// 长度校验
		if(strlen($content) > 255) {
			return View::error('内容超过了长度限制，最长是140个字');
		}
		
		$uin = SessionInfo('uin');
		
		loadLib('Message');
		$code = Message::add($owner_uin , $uin , $content);
		
		if($code === 0) {
			// 发送系统消息，通知对方
			if($owner_uin != $uin) {
				loadLib('Operate');
				Operate::getAdd($uin , $owner_uin , 'message_reply');
				loadLib('SysMsg');
				SysMsg::sendSystem($owner_uin , array(
					'send_uin' => $uin,
					'id' => Message::$message_id,
				) , 1);
			}
			
			return View::success('发表留言成功');
		} else {
			return View::error('系统繁忙，请稍后再试' , $code);
		}
	}
	
	/**
	 * 回复留言界面
	 */
	function reply() {
		$owner_uin = isset($_GET['uin']) ? intval($_GET['uin']) : null;
		$id = isset($_GET['id']) ? intval($_GET['id']) : null;
		
		if(empty($owner_uin) || empty($id)) {
			return View::error('参数错误');
		}
		
		loadLib('Message');
		$message = Message::getOne($owner_uin , $id);
		if(empty($message['id'])){
			return View::error('该投诉信息已被删除' );
		}
		$send_uin_list = array();
		$send_uin_list[] = $message['send_uin'];
		if(!empty($message['reply'])) {
			foreach($message['reply'] as $reply_row) {
				$send_uin_list[] = $reply_row['uin'];
			}
		}
		
		if(!empty($send_uin_list)) {
			loadLib('User');
			$send_uin_info = User::getBaseInfo($send_uin_list);
			
			$send_uin = $message['send_uin'];
			$message['send_uin_name'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['name'] : '';
			$message['send_uin_sex'] = isset($send_uin_info[$send_uin]) ? $send_uin_info[$send_uin]['sex'] : '';
			
			if(!empty($message['reply'])) {
				foreach($message['reply'] as $j => $reply_row) {
					$reply_uin = $reply_row['uin'];	
					$message['reply'][$j]['name'] = isset($send_uin_info[$reply_uin]) ? $send_uin_info[$reply_uin]['name'] : '';
					$message['reply'][$j]['sex'] = isset($send_uin_info[$reply_uin]) ? $send_uin_info[$reply_uin]['sex'] : '';
				}
			}
		}
		
	
		// 准备个人主页导航数据
		View::prepare_profile_nav_data($owner_uin);
		
		$smarty = loadSmarty();
		$smarty->assign('message' , $message);
		$smarty->assign('owner_uin' , $owner_uin);
		$smarty->assign('id' , $id);
		
		return $smarty->fetch('message/reply.tpl.php');
	}
	
	/**
	 * 回复留言
	 */
	function doreply() {
		$owner_uin = isset($_POST['owner_uin']) ? intval($_POST['owner_uin']) : null;
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		$content = isset($_POST['content']) ? trim($_POST['content']) : null;
		
		if(empty($owner_uin) || empty($id)) {
			return View::error('参数错误');
		}
		
		// 检查留言有效性
		if($content === null || strlen($content) == 0) {
			return View::error('没有填写内容');
		}
		
		// 长度校验
		if(strlen($content) > 255) {
			return View::error('内容超过了长度限制，最长是140个字');
		}
		
		$uin = SessionInfo('uin');
		
		loadLib('Message');
		$org_message = Message::getOne($owner_uin , $id);
		/*if(empty($org_message)) {
			return View::error('该留言已被删除' , $code);
		}*/
		
		if(empty($org_message['id'])){
			return View::error('该信息已被删除' );
		}
		$code = Message::reply($owner_uin , $uin , $id , $content , $org_message);
		
		if($code === 0) {
			// 发送系统消息，通知对方
			if($org_message['send_uin'] != $org_message['uin']) {
				loadLib('Operate');
				Operate::getAdd($uin , $org_message['uin'] , 'message_reply');
				$sysmsg_to_uin = $uin == $org_message['uin'] ? $org_message['send_uin'] : $org_message['uin'];
				loadLib('SysMsg');
				SysMsg::sendSystem($sysmsg_to_uin , array(
					'send_uin' => $uin,
					'owner_uin' => $owner_uin,
					'id' => $id,
				) , 2);
			}
			
			return View::success('回复留言成功');
		} else if($code === 404) {
			return View::error('该留言已被删除' , $code);
		} else {
			return View::error('系统繁忙，请稍后再试' , $code);
		}
	}
	
	/**
	 * 删除留言
	 */
	function delete() {
		$id = isset($_POST['id']) ? intval($_POST['id']) : null;
		
		if(empty($id)) {
			return View::error('参数错误');
		}
		
		$uin = SessionInfo('uin');
		
		loadLib('Message');
		$code = Message::delete($uin , $id);
		
		if($code === 0) {
			return View::success('删除留言成功');
		} else {
			return View::error('系统繁忙，请稍后再试' , $code);
		}
	}
	
}

# end of script
